<?php

namespace app\model;
use app\model\UserInfo as ModelUserInfo;

class UserPassword extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'user_password';
    }

    /**
     * 类入口
     * @return UserPassword
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 设置登录密码
     * @param $int_user_id
     * @param $str_password
     * @return bool
     */
    public function setUserPassword($int_user_id, $str_password)
    {
        $arr_info = $this->toFind(['user_id' => $int_user_id], 'id');
        $str_pwd_key = $this->_getRandomString(8);
        $arr_data = [
            'password' => passwordEncryption($str_password, $str_pwd_key),
            'pwd_key' => $str_pwd_key,
        ];
        if (isset($arr_info['id'])) {
            $this->toUpdate(['id' => $arr_info['id']], $arr_data);
        } else {
            $arr_data['user_id'] = $int_user_id;
            $this->toAdd($arr_data);
        }

        return true;
    }

    /**
     * 检查是否设置国支付密码
     * @param $int_user_id
     * @return int
     */
    public function checkIsSetLoginPwd($int_user_id)
    {
        $arr_info = ModelUserInfo::getInstance()->toFind(['user_id' => $int_user_id], 'user_id,password');
        if (!empty($arr_info['password'])) {
            return 1;
        } else {
            return 0;
        }
    }
}
